<?php

namespace App\Http\Controllers\Wechat;


class MenuController extends HelpController
{

    /**
     * 获取微信公众号菜单列表
     * @return \EasyWeChat\Support\Collection
     *
     */
    public function getMenu()
    {
        try{
            $menulist = $this->app->menu->all();
            return $this->success($menulist,'请求成功');
        }catch (\Exception $e){
            return $this->fail('获取菜单失败'.$e->getMessage());
        }
    }

    /**
     * 创建微信公众号菜单
     * @return array
     */

    public function createMenu()
    {
        $buttons = [
            [
                "name" => "课程产品",
                "sub_button" => [
                    [
                        "type" => "view",
                        "name" => "最新课程",
                        "url"  => $this->hosturl."/courses_m.html"
                    ],
                    [
                        "type" => "view",
                        "name" => "名师介绍",
                        "url"  => $this->hosturl."/teachers_m.html"
                    ]
                ]
            ],
            [
                "type" => "view",
                "name" => "我的课表",
                "url"  => $this->hosturl."/wechat/authorize/schedule"
            ],
            [
                "type" => "view",
                "name" => "个人中心",
                "url"  => $this->hosturl."/wechat/authorize/homepage"
            ]
        ];
        try{
            $result = $this->app->menu->add($buttons);
            if($result['errcode'] == 0){
                return $this->success(true,'创建成功');
            }else{
                return $this->fail(false,'创建失败'.$result['errcode'].$result['errmsg']);
            }
        }catch (\Exception $e){
            return $this->fail(false,'创建菜单失败'.$e->getMessage());
        }


    }

    public function deleteMenu($id = null)
    {
        try{
            $result = $this->app->menu->destroy($id);
            if($result['errcode'] == 0){
                return $this->success(true,'删除成功');
            }else{
                return $this->fail(false,'删除失败'.$result['errcode'].$result['errmsg']);
            }
        }catch (\Exception $e){
            return $this->fail(false,'删除菜单失败'.$e->getMessage());
        }

    }

}
